<?php

namespace app\service\shop;

use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Payment\Wap\Models\AlipayTradeWapPayResponse;
use app\tools\Logs;
use think\Exception;

/**
 * 商城支付
 * User : Du
 * Date : 2022/11/27
 * Time : 21:54
 * Class PayServices
 * @package projects\services\shop
 */
class PayServices
{

    /**
     * 支付宝发起支付
     * User : Du
     * Date : 2022/11/30
     * Time : 16:12
     * @param array $request
     * @return false|string
     */
    public static function ZfbPay(array $request)
    {

        $subject = 'iPhone14 pro 8+512G';
        //商户订单号 最长64
        $outTradeNo = getOrderNo();;
        //金额
        $total = '8799.00';
        //1. 设置参数（全局只需设置一次）
        Factory::setOptions(self::getOptions());
        try {
            //获取支付信息页面
            $result = Factory::payment()->page()->pay($subject, $outTradeNo, $total, "https://tp.lingyun.pro/shop/cart");
            $responseChecker = new ResponseChecker();
            //3. 处理响应或异常
            if ($responseChecker->success($result)) {
                return $result->body;
            } else {
                Logs::error('支付宝发起失败', ['data' => $result], 'aliPayError');
                return false;
            }
        } catch (\Exception $e) {
            Logs::error('支付宝配置错误', ['data' => $e->getMessage()], 'aliPayError');
            return false;
        }
    }

    /**
     * 支付宝配置初始化
     * User : Du
     * Date : 2022/11/30
     * Time : 12:14
     * @return Config
     */
    private static function getOptions(): Config
    {
        $options = new Config();
        $options->protocol = 'https';
        //支付宝网关
        $options->gatewayHost = 'openapi.alipaydev.com';
//        $options->gatewayHost = 'openapi.alipay.com';
        //加密方式
        $options->signType = 'RSA2';
        //AppId
        $options->appId = '2016102100733286';
        //应用私钥
        $options->merchantPrivateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDUJ5vSB65T1FmBEVm9t+aFOu9WKTn3xGxuE6NTpu/EkR3u03dEsgoz1s1C0wc8QElY0HYstX3sDfypPbFTGJDUDg58UO6UcYz8tArA8r/VnSaoc5vldnhOe91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iK3VWIW/OdSKmpuaKzoA4yVnAvJ72CEMCgYBMxBy218nErytYzC0CogcUUkdEVBLX1d2aWe+M1HK4Gcbj6GF2Yj8TY0srs2ggDuuarfINwH91n1KkvdlGrVD6vq2XsGoesxFTZsMx/bIcH4h2+IzB8hOsFlFeEZTGyezVcs8CCjLM6Pze/lkPV2YMaFlP5H3RoNqNd3o4oMbE1QKBgGa+nIpbI0aItVBGXD2qj7n9UQuPcYsFxSyFW0ay9wXT5BMkdnL0N2HMECPCgm8GzaTjuhSiG2x0XzBrKMgfZqJhoYRoxrF5Kp3KDe5Y+by1tgJqeboU7v1Gr3cHPwxqjCthW3WfEe8bQoo/xE7jgpIyLDA6fGVIiwXJTdAfMNBe';

//        //注：如果采用非证书模式，则无需赋值三个证书路径，改为赋值如下的支付宝公钥字符串即可
//        $options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->';
//        $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt" -->';
//        $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->';
        //支付宝公钥
        $options->alipayPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3/XXXJYFFPjs5zczZ0GUSdY36KJiwaEvXmysQviK3ppWIvZQR3uM1czmCuC0DtohaBDQUcrw4DGFHpfc4/2649Devx2jaRt8GUQLYjsya7lE18TVTNGyYrjwLRHYJaweT4lXMDKz1dFZeP8lnPeA1BPryNpjucmaCLR1SCIaAZh+sGsKxK91oi2kconIHrrLX/v83iC/8mZmz+oATyq14IuUQgUsd9nRMFz5FvGQt8KfSoRLEBs4Hn3UM+9zo7HYeqVIjIO+55cPpGFVVsqE9/0XLrv0uD+dwAF4v1q7izsreg+CpSvW6aSSKEwt5y+1RV4TLJC9KHU4lWLFYX5QJwIDAQAB';
        //可设置异步通知接收服务地址（可选）
        $options->notifyUrl = "https://tp.lingyun.pro/aliPayCallback";
//        $options->returnUrl = "https://tp.lingyun.pro/shop/aliPay/return";

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
//        $options->encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";

        return $options;
    }
}